package p.ithorns.framework.common.tree;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * TreeNode
 *
 * 树节点对象
 *
 * @author liuhaitao
 * @version 1.0
 * @date 2022/3/7 15:04
 */
public class TreeNode<E> implements Serializable {

    /**
     * 节点ID
     */
    private Serializable id;

    /**
     * 父节点ID
     */
    private Serializable pid;

    /**
     * 节点标签
     */
    private String label;

    /**
     * 节点层级
     */
    private int level;

    /**
     * 节点类型
     */
    private String type;

    /**
     * 节点扩展数据对象
     */
    private E ext;

    /**
     * key
     */
    private String key;

    /**
     * 子节点列表
     */
    private List<TreeNode> children;

    public void add(TreeNode treeNode) {
        children.add(treeNode);
    }

    public void calKey() {
        this.key = this.type + "-" + this.id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof TreeNode)) {
            return false;
        }

        TreeNode<?> treeNode = (TreeNode<?>) o;

        if (level != treeNode.level) {
            return false;
        }
        if (!Objects.equals(id, treeNode.id)) {
            return false;
        }
        if (!Objects.equals(pid, treeNode.pid)) {
            return false;
        }
        if (!Objects.equals(label, treeNode.label)) {
            return false;
        }
        return Objects.equals(type, treeNode.type);

    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (pid != null ? pid.hashCode() : 0);
        result = 31 * result + (label != null ? label.hashCode() : 0);
        result = 31 * result + level;
        result = 31 * result + (type != null ? type.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "{" +
                "'id':'" + id +'\'' +
                ", 'pid':'" + pid +'\'' +
                ", 'label':'" + label + '\'' +
                ", 'level':" + level +
                ", 'ext':" + ext +
                ", 'children':" + children +
                "}";
    }

    public Serializable getId() {
        return id;
    }

    public Serializable getPid() {
        return pid;
    }

    public String getLabel() {
        return label;
    }

    public int getLevel() {
        return level;
    }

    public String getType() {
        return type;
    }

    public E getExt() {
        return ext;
    }

    public String getKey() {
        return key;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setId(Serializable id) {
        this.id = id;
    }

    public void setPid(Serializable pid) {
        this.pid = pid;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setExt(E ext) {
        this.ext = ext;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }
}